<?php
/**
 * Created by PhpStorm.
 * User: 王凯林
 * Date: 18/3/27
 * Time: 10:13
 */

namespace App\Http\Controllers\Admin;


use App\Common\Exceptions\AixinException;
use App\Common\Exceptions\AixinExceptionCode;
use App\Http\Controllers\Controller;
use App\Model\CashRecord;
use App\Model\MoneyReport;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class CashRecordController extends Controller
{
    /*
     * 所有会员的提现记录
     */
    public function index(Request $request){
        $queryStr = $request->querystr;
        $datas = CashRecord::where('account', 'like', "{$queryStr}%")
            ->orWhere('account_name', 'like', "{$queryStr}%")
            ->with('getUidName')
            ->orderBy('create_time','desc')
            ->paginate(10);
        return view('admin.cashrecord.index',['datas'=>$datas,'querystr'=>$queryStr]);

    }
    /*
     * 提现记录的后台审核
     */
    public function updateStatus(Request $request){
        if($request->has('id')&& $request->has('uid') && $request->has('status') && $request->has('amount')){
            DB::beginTransaction();
            try{
                $re1 = CashRecord::where('id', $request->id)
                    ->update(['status' => $request->status,'update_time'=>time()]);
                //=====操作money_report表updata>>status=====
                    /*  $uid = $request->uid;
                $amount = '-'.$request->amount;
                $data=[
                    'types'=>5,
                    'status'=>1,
                    'uid'=>$uid,
                    'amount'=>$amount,
                    'create_time'=>time(),
                    'remark'=>'提现'
                ];*/
                $re2 = MoneyReport::where('uid',$request->uid)
                    ->update(['status'=>1,'update_time'=>time(),'remark'=>'提现成功']);
                //=====操作money_report表updata>>status=====
                if ($re1 && $re2) {
                    DB::commit();
                    return ApiResponse([]);
                }
            }catch(\Exception $e){
                DB::rollBack();
                return ApiResponse([], '修改失败', 1);
            }
        }else{
            throw new AixinException(AixinExceptionCode::MISSING_PARAM);
        }


    }

}